<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\CardsPictureIntroductions;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

/**
 * 图文介绍
 * Class CardsPictureIntroductionsController
 * @package App\Http\Controllers\Front
 */
class CardsPictureIntroductionsController extends Controller
{
    /**
     * @var User
     */
    private $user = null;
    public function __construct()
    {
        $this->user = Auth::user();
    }

    /**
     * 获取当前用户图文介绍列表
     * @param Request $request
     * @param CardsPictureIntroductions $pictureIntroductions
     * @return Response
     */
    public function index(Request $request, CardsPictureIntroductions $pictureIntroductions)
    {
        $type = $request->get('type',1);

        $pictures = $pictureIntroductions->where('user_id', '=', $this->user->id)->where('type', $type)->get();
        $data = [];
        foreach ($pictures as $picture) {
            $data[] = [
                'id' => $picture->id,
                'title' => $picture->title,
                'type' => $picture->type,
                'created_at' => $picture->created_at->format('Y-m-d')
            ];
        }
        return $this->renderJson($data);
    }

    /**
     * 获取单个图文详情
     * @param int $id
     * @param CardsPictureIntroductions $cardsPictureIntroductions
     * @return Response
     */
    public function show($id, CardsPictureIntroductions $cardsPictureIntroductions)
    {
        $picture = $cardsPictureIntroductions->findOrFail($id);
        $data = [
            'id' => $picture->id,
            'type' => $picture->type,
            'title' => $picture->title,
            'content' => $picture->content,
            'images' => $picture->picture_path,
            'created_at' => $picture->created_at->format('Y-m-d')
        ];
        return $this->renderJson($data);
    }

    /**
     * 添加图文
     * @param Request $request
     * @param CardsPictureIntroductions $pictureIntroductions
     * @return Response
     */
    public function store(Request $request, CardsPictureIntroductions $pictureIntroductions)
    {

        $this->validate($request, [
            'type' => 'required',
            'title' => 'required',
            'content' => 'required',
            'images' => 'required'
        ], [
            'required' => ':attribute不能为空'
        ], [
            'type' => '介绍类型',
            'title' => '标题',
            'content' => '内容',
            'images' => '图片'
        ]);


        $type = $request->input('type');
        if (in_array($type, [1, 2])){
            $my = $pictureIntroductions->where(['user_id'=>$this->user->id,'type'=>$type])->count();
            if ($my >= 1){
                return $this->fail('该分类下只能添加一条');
            }
        }
        $picture = new CardsPictureIntroductions();
        if (!$picture->add($request)) {
            return $this->error('添加失败');
        }
        return $this->success();
    }

    public function update(Request $request, $id, CardsPictureIntroductions $pictureIntroductions)
    {
        $picture = $pictureIntroductions->findOrFail($id);
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required',
            'images' => 'required'
        ], [
            'required' => ':attribute不能为空'
        ], [
            'title' => '标题',
            'content' => '内容',
            'images' => '图片'
        ]);

        if (!$picture->edit($request)) {
            return $this->error('修改失败');
        }
        return $this->success();
    }

    public function destroy($id, CardsPictureIntroductions $pictureIntroductions)
    {
        $cardPic = $pictureIntroductions->findOrFail($id);
        try {
            $res = $cardPic->delete();
            if ($res){
                return $this->success();
            }
        } catch (\Exception $e) {
        }
        return $this->error('删除失败');
    }
}
